package cn.edu.scau.cmi.zenghanxiong.composite.safe;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.fxml.Initializable;

public class SafeController implements Initializable{
	private SafeComposite boss = new SafeComposite("boss");
    private Map<String, SafeComponent> map = new HashMap<>();
    private String parentName = "";
    private String nodeName = "";
    {
        this.map.put(boss.getName(), boss);
    }

    @FXML
    private TextArea output;
    @FXML
    private TextField input1;
    @FXML
    private TextField input2;
    @FXML
    private Button addF;
    @FXML
    private Button addL;
    @FXML
    private Button delete;

    public void addComposite() {
        if (checkInput()) {
            SafeComposite parent = this.getParent(parentName);
            if (parent == null) {
                return;
            }
            if (map.containsKey(nodeName)) {
                this.output.appendText("该命名已存在，请重新输入！\n");
                return;
            }
            SafeComponent node = new SafeComposite(nodeName);
            parent.addChild(node);
            this.map.put(node.getName(), node);
            this.output.clear();
            this.display(boss, 0);
        }
    }

    public void addLeaf() {
        if (checkInput()) {
            SafeComposite parent = this.getParent(parentName);
            if (parent == null) {
                return;
            }
            if (map.containsKey(nodeName)) {
                this.output.appendText("该命名已存在，请重新输入！\n");
                return;
            }
            SafeComponent node = new SafeLeaf(nodeName);
            parent.addChild(node);
            this.map.put(node.getName(), node);
            this.output.clear();
            this.display(boss, 0);
        }
    }

    public void remove() {
        if (checkInput()) {
            SafeComposite parent = this.getParent(parentName);
            if (parent == null) {
                return;
            }
            SafeComponent node = map.getOrDefault(nodeName, null);
            if (node == null) {
                this.output.appendText("不存在该节点！\n");
                return;
            }
            if (node == boss) {
                this.output.appendText("不能删除 root 节点\n");
                return;
            }
            parent.removeChild(node);
            this.map.remove(nodeName);
            this.output.clear();
            this.display(boss, 0);
        }
    }

    private void display(SafeComponent boss, int level) {
        if (level == 0) {
            this.output.appendText(boss.toString() + "\n");
        } else {
            for (int i = 0; i < level; i++) {
                this.output.appendText("\t");
            }
            this.output.appendText("--" + boss.toString() + "\n");
        }
        if (boss instanceof SafeComposite) {
            for (SafeComponent child : ((SafeComposite) boss).getChildren()) {
                this.display(child, level + 1);
            }
        }
    }

    private SafeComposite getParent(String name) {
        SafeComponent parent = map.getOrDefault(name, null);
        if (parent instanceof SafeLeaf || parent == null) {
            this.output.appendText("输入错误，该节点不是父节点或节点不存在。\n");
            return null;
        }
        return (SafeComposite) parent;
    }

    private boolean checkInput() {
        this.parentName = input1.getText();
        this.nodeName = input2.getText();
        if ("".equals(this.parentName) || "".equals(this.nodeName)) {
            this.output.appendText("请完整输入父节点及操作节点！" + "\n");
            return false;
        }
        return true;
    }
    public void initialize(URL location, ResourceBundle resources) {
        this.display(boss, 0);
    }

}
